package com.swjd.jingle.fragment;

import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.SearchView;

import com.google.gson.Gson;
import com.hxl.okhttp.response.JsonArrayResponseHandler;
import com.swjd.jingle.R;
import com.swjd.jingle.adapter.LvAdapter;
import com.swjd.jingle.pojo.Goods;
import com.swjd.jingle.util.Constant;
import com.xuexiang.xui.widget.toast.XToast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectGoodsFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.sv_search)
    SearchView svSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.lv)
    ListView lv;
    private String input;
    private LvAdapter lvAdapter;
    private List<Goods> goods = new ArrayList<>();

    public void setInput(String input) {
        this.input = input;
    }

    @Override
    public void initialize() {
        lvAdapter = new LvAdapter(activity, goods);
        lv.setAdapter(lvAdapter);
        svSearch.setQuery(input, false);
        selectGoods(input);
    }

    @Override
    public void initViews() {
    }

    @Override
    public void initData() {

    }

    public void selectGoods(String input) {
        dialog.show();
        Map<String, String> params = new HashMap<>();
        params.put("input", input);
        app.getOkHttpUtils().post(Constant.BASE_URL + Constant.queryGoods, params, new JsonArrayResponseHandler() {
            @Override
            public void onSuccess(int statusCode, JSONArray response) {
                try {
                    goods.clear();
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject data = (JSONObject) response.get(i);
                        Goods good = new Gson().fromJson(data.toString(), Goods.class);
                        goods.add(good);
                    }
                    lvAdapter.notifyDataSetChanged();
                    if (goods.size() == 0) {
                        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                        XToast.info(activity, "查询不到商品数据!", Toast.LENGTH_SHORT).show();
                    } else {
                        svSearch.clearFocus();
                        XToast.success(activity, "查询成功!", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.cancel();
            }

            @Override
            public void onFailure(int statusCode, String error_msg) {
                if (statusCode == 200) {
                    XToast.error(activity, "查询不到商品数据!", Toast.LENGTH_SHORT).show();
                    dialog.cancel();
                } else {
                    XToast.error(activity, "网络请求失败!", Toast.LENGTH_SHORT).show();
                }
                dialog.cancel();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_select_goods;
    }


    @Override
    @OnClick({R.id.img_back, R.id.tv_search})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                try {
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            fragmentManager.popBackStack();
                        }
                    }, 500);
                }
                break;
            case R.id.tv_search:
                input = svSearch.getQuery().toString();
                selectGoods(input);
                break;
        }
    }

}